﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyClassServer.Net;

namespace EasyClassServer.Application.Log
{
    class Login : IApplication
    {
        public string Name => "login";

        public HttpResponseArgs Handle(HttpArgs args)
        {
            LoginModel model = new LoginModel();
            var email = (string)args.GetPostValue()["email"];
            var pwd = (string)args.GetPostValue()["pwd"];
            var loging_user = model.Select("email", email);
            if(!model.Find("email",email))
            {
                return new HttpResponseArgs
                {
                    Code = HttpCode.Error,
                    Message = "账号不存在",
                    Data = new LoginModel
                    {
                        Error = LoginModel.ErrorType.AccountNotExit,
                        Token = ""
                    }
                };
            }
            if ((string)loging_user[0]["pwd"] == pwd)
            {
                var token = model.GenerateTokenString();
                Data.CacheManager.SetCache(email, token);
                return new HttpResponseArgs
                {
                    Code = HttpCode.Sucsess,
                    Message = "验证成功",
                    Data = new LoginModel
                    {
                        Error = LoginModel.ErrorType.Success,
                        Token = token
                    }
                };
            }
            else
            {
                return new HttpResponseArgs
                {
                    Code = HttpCode.Error,
                    Message = "密码错误",
                    Data = new LoginModel
                    {
                        Error = LoginModel.ErrorType.PasswordWrong,
                        Token = ""
                    }
                };
            }
        }
    }
}
